<template>
  <a-drawer
    :title="title"
    placement="right"
    :closable="true"
    :width="width"
    :visible="visible"
    @close="handleCancel"
    destroyOnClose
  >
    <a-spin :spinning="confirmLoading">
      <j-form-container>
        <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
          <a-row>
            <a-col :span="24">
              <a-form-model-item label="仓库名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="depositoryName">
                <a-input v-model="model.depositoryName" placeholder="请输入仓库名称"  ></a-input>
              </a-form-model-item>
            </a-col>
            <!--<a-col :span="24">
              <a-form-model-item label="仓库种类数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="depositoryKindNum">
                <a-input-number v-model="model.depositoryKindNum" placeholder="请输入仓库种类数量" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="仓库材料数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="depositoryMaterialNum">
                <a-input-number v-model="model.depositoryMaterialNum" placeholder="请输入仓库材料数量" style="width: 100%" />
              </a-form-model-item>
            </a-col>-->
            <a-col :span="24">
              <a-form-model-item label="仓库状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="depositoryStatus">
                <a-radio-group button-style="solid" v-model="model.depositoryStatus">
                  <a-radio-button :value="1">
                    启用
                  </a-radio-button>
                  <a-radio-button :value="2">
                    停用
                  </a-radio-button>
                </a-radio-group>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="预警数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inventoryWarningNum">
                <a-input v-model="model.inventoryWarningNum" placeholder="请输入预警数量"  ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="预警设置" prop="informantType" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-radio-group button-style="solid" v-model="model.informantType" @change="onChange">
                  <a-radio-button :value="1">
                    部门
                  </a-radio-button>
                  <a-radio-button :value="2">
                    用户
                  </a-radio-button>
                </a-radio-group>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item v-if="model.informantType == 1" label="选择部门" prop="informantId" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <j-select-depart v-model="model.informantId" :multi="true"></j-select-depart>
              </a-form-model-item>
              <a-form-model-item v-if="model.informantType == 2" label="选择用户" prop="informantId" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <j-select-user-by-dep v-model="model.informantId" :multi="true"></j-select-user-by-dep>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </j-form-container>
    </a-spin>
    <a-button type="primary" style="width: 100%" @click="handleOk">保存</a-button>
  </a-drawer>

  <!--<j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :maskClosable="false"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    @cancel="handleCancel"
    cancelText="关闭">

  </j-modal>-->
</template>

<script>
  import { httpAction } from '@/api/manage'
  export default {
    name: 'depositoryModal',
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        title:'',
        width: '100%',
        visible: false,
        disableSubmit: false,
        model:{},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          depositoryName:[
            { required: true, message: '仓库名称不能为空' },
          ],
          depositoryKindNum:[
            { required: true, message: '仓库种类数量不能为空' },
            {"pattern": /^[0-9]*$/,"message": "只能输入正整数"},
          ],
          depositoryMaterialNum:[
            { required: true, message: '仓库材料数量不能为空' },
            {"pattern": /^[0-9]*$/,"message": "只能输入正整数"},
          ],
          inventoryWarningNum:[{ required: true, message: '预警数量不能为空' },],
          informantId:[{ required: true, message: '请选择预警通知用户' },]
        },
        url: {
          add: "/ghSupplychainDepository/add",
          edit: "/ghSupplychainDepository/edit",
          queryById: "/ghSupplychainDepository/queryById"
        }
      }
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        //this.model.informantType = 1;
        this.visible = true;
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              this.close();
            })
          }

        })
      },
      submitCallback(){
        this.$emit('ok');
        this.visible = false;
      },
      handleCancel () {
        this.close()
      },
      onChange(e) {
        this.model.informantType = e.target.value
      }
    }
  }
</script>